package com.ruoyi.dataserver.line.service.impl;

import com.ruoyi.dataserver.line.dto.LineFileAndOperationSymbolAndValueDto;
import com.ruoyi.dataserver.line.service.LineService;
import com.ruoyi.dataserver.line.utils.LineSqlUtil;
import com.ruoyi.dataserver.line.utils.PageUtil;
import com.ruoyi.dataserver.line.entity.Line;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ProjectName intelligent-city
 * @Package com.lab.dataserver.line.service.impl
 * @ClassName LineServiceImpl
 * @Description 管线查询service
 * @Company lab
 * @Author zgq
 * @Date 2020/7/6 10:40
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Service
public class LineServiceImpl implements LineService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public PageUtil pageSelect(String lineTableName, List<LineFileAndOperationSymbolAndValueDto> lovList, Long current, Long size) {
        Long total = jdbcTemplate.queryForObject(LineSqlUtil.countSql(lineTableName, lovList), Long.class);
        PageUtil pageUtil = new PageUtil(current, size, total);
        RowMapper<Line> rowMapper = new BeanPropertyRowMapper<Line>(Line.class);
        List<Line> records = jdbcTemplate.query(LineSqlUtil.pageSelectSql(lineTableName, lovList, pageUtil), rowMapper);
        pageUtil.setRecords(records);
        return pageUtil;
    }

    @Override
    public PageUtil pageSelect2(String lineTableName, List<LineFileAndOperationSymbolAndValueDto> lovList, Long current, Long size) {
        Long total = jdbcTemplate.queryForObject(LineSqlUtil.countSql2(lineTableName, lovList), Long.class);
        PageUtil pageUtil = new PageUtil(current, size, total);
        RowMapper<Line> rowMapper = new BeanPropertyRowMapper<Line>(Line.class);
        List<Line> records = jdbcTemplate.query(LineSqlUtil.pageSelectSql2(lineTableName, lovList, pageUtil), rowMapper);
        pageUtil.setRecords(records);
        return pageUtil;
    }
    @Override
    public PageUtil pageSelect4(String lineTableName, List<LineFileAndOperationSymbolAndValueDto> lovList, Long current, Long size) {
        Long total = jdbcTemplate.queryForObject(LineSqlUtil.countSql3(lineTableName, lovList), Long.class);
        PageUtil pageUtil = new PageUtil(current, size, total);
        RowMapper<Line> rowMapper = new BeanPropertyRowMapper<Line>(Line.class);
        List<Line> records = jdbcTemplate.query(LineSqlUtil.pageSelectSql3(lineTableName, lovList, pageUtil), rowMapper);
        pageUtil.setRecords(records);
        return pageUtil;
    }


    @Override
    public Map<String, Object> pageSelect3(String lineTableName, List<LineFileAndOperationSymbolAndValueDto> lovList, Long current, Long size) {
        Long total = jdbcTemplate.queryForObject(LineSqlUtil.countSql3(lineTableName, lovList), Long.class);
        PageUtil pageUtil = new PageUtil(current, size, total);
        RowMapper<Line> rowMapper = new BeanPropertyRowMapper<Line>(Line.class);
        //这里查询的是所有数据(0->total)
        List<Line> recordsAll = jdbcTemplate.query(LineSqlUtil.pageSelectSql3(lineTableName, lovList, pageUtil), rowMapper);
        //截取索引值的一些判断判断(防止报错)
        int fromIndex = LineSqlUtil.getBegin(pageUtil) > pageUtil.getTotal() ? pageUtil.getTotal().intValue() : LineSqlUtil.getBegin(pageUtil).intValue();
        int toIndex = LineSqlUtil.getEnd(pageUtil) > pageUtil.getTotal() ? pageUtil.getTotal().intValue() : LineSqlUtil.getEnd(pageUtil).intValue();
        fromIndex = fromIndex > 0 ? fromIndex : 0;
        toIndex = toIndex > fromIndex ? toIndex : fromIndex;
        //从recordsAll中截取当前页数的数据
        List<Line> records = recordsAll.subList(fromIndex, toIndex);
        pageUtil.setRecords(records);
        //获取所有recordsAll的usid
        List<String> usIdList = recordsAll.stream().map(line -> line.getUsId()).collect(Collectors.toList());
        //返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("current", pageUtil.getCurrent());
        result.put("size", pageUtil.getSize());
        result.put("total", pageUtil.getTotal());
        result.put("pages", pageUtil.getPages());
        result.put("records", pageUtil.getRecords());
        result.put("usIdList", usIdList);
        return result;
    }
}
